//
//  FYStatusRetweetView.swift
//  Mircore
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit


import SnapKit
//转发微博视图

class FYStatusRetweetView: UIView {
    
    //记录转发微博视图的底部约束
    
    var  retweetViewBottomConstraint: Constraint?
    
    //使用ViewModel绑定数据
    
    var  statusViewModel: FYStatusViewModel? {
        
        didSet  {
            
            //绑定转发微博内容
            
            contentLabel.text = statusViewModel?.retweetContent
            
            //卸载上一个约束
            
            retweetViewBottomConstraint?.uninstall()
            
            //判断配图是否存在并且是否大于0
            
            if  let  picUrls  = statusViewModel?.status?.retweeted_status?.pic_urls  where  picUrls.count > 0 {
                
                //显示配图
                
                pictureView.hidden  = false
                
                //设置配图信息
                
                pictureView.picUrls = picUrls
                
                //更新约束
                
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.retweetViewBottomConstraint  = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                    
                })
                
            }else {
                
                
                //隐藏视图
                
                pictureView.hidden  = true
                
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.retweetViewBottomConstraint  = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
            
        }
        
    }
    
    //MARK:-懒加载控件
    
    private lazy  var  contentLabel: UILabel = {
        
        let  label  = UILabel(fontSize: StatusFontSize, textColor: UIColor.grayColor())
        
        //多行显示
        
        label.numberOfLines  = 0
        
        return label
        
    }()
    
    //配图视图
    
    private  lazy  var  pictureView: FYStatusPictureView = {
        
        let  view  = FYStatusPictureView()
        
        view.backgroundColor   = self.backgroundColor
        
        return view
        
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has  not been  implemented")
    }
    
    
    //添加控件设置约束
    
    private  func  setupUI() {
        
        backgroundColor  = UIColor(white: 0.95, alpha: 1)
        
        //添加控件
        
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        
        //设置约束
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            
            make.trailing.equalTo(self).offset(StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
        }
        
        //关键约束
        
        self.snp_makeConstraints { (make) in
            
            self.retweetViewBottomConstraint  = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    }
    
}
